from django.shortcuts import render
import re, urllib.parse, urllib.request, json, time
from django.http import HttpResponse
from email.mime.text import MIMEText
from email.header import Header
from smtplib import SMTP_SSL
from common.user.code import codes
# 导入邮箱验证码数据
from users.models import *


def login_email(receiver):
    # qq邮箱smtp服务器
    host_server = 'smtp.qq.com'
    # sender_qq为发件人的qq号码
    sender_qq = '2224693191'
    # pwd为qq邮箱的授权码
    pwd = 'fhiskioisjdqeacg'
    # 发件人的邮箱
    sender_qq_mail = '2224693191@qq.com'
    # 收件人邮箱
    receiver = receiver
    # 邮件的正文内容
    mail_content = f'<h1>欢迎使用悟空商品</h1><br>您正在绑定QQ邮箱，验证码为：{codes()}， 若非本人操作，请勿泄露。'
    # 邮件标题
    mail_title = '绑定QQ邮箱'
    # ssl登录
    smtp = SMTP_SSL(host_server)
    # set_debuglevel()是用来调试的。参数值为1表示开启调试模式，参数值为0关闭调试模式
    smtp.set_debuglevel(1)
    smtp.ehlo(host_server)
    smtp.login(sender_qq, pwd)

    msg = MIMEText(mail_content, "html", 'utf-8')
    msg["Subject"] = Header(mail_title, 'utf-8')
    msg["From"] = sender_qq_mail
    msg["To"] = receiver
    smtp.sendmail(sender_qq_mail, receiver, msg.as_string())
    smtp.quit()


# 邮箱验证码保存起来
def sendemail(email):
    # 生成验证码
    code_num = codes()
    # 过期时间
    time_num = int(time.time()) + 60
    # 判断数据库是否发过这个邮箱的验证码
    if email_code.objects.filter(email=email).exists():
        # 修改验证码和有效期
        dq_email = email_code.objects.get(email=email)
        dq_email.code = code_num  # 验证码
        dq_email.time = time_num  # 时间
        dq_email.save()  # 保存
        # 发送验证码
        emailCode(email, code_num)
        return True
    else:
        # 没有发过就添加
        email_code(email=email, code=code_num, time=time_num).save()
        # 发送验证码
        emailCode(email, code_num)
        return True


# 发送验证码
def emailCode(qq_email, code):
    # qq邮箱smtp服务器
    host_server = 'smtp.qq.com'
    # sender_qq为发件人的qq号码
    sender_qq = '2224693191'
    # pwd为qq邮箱的授权码
    pwd = 'xbvfwpzvxmedeaec'
    # 发件人的邮箱
    sender_qq_mail = '2224693191@qq.com'
    # 收件人邮箱
    receiver = qq_email
    # 邮件的正文内容
    mail_content = f'[悟空商城] 验证码为：{code}, 您正在绑定QQ邮箱，验证码1分钟内有效，若非本人操作，请勿泄露。'
    # 邮件标题
    mail_title = '绑定QQ邮箱'
    # ssl登录
    smtp = SMTP_SSL(host_server)
    # set_debuglevel()是用来调试的。参数值为1表示开启调试模式，参数值为0关闭调试模式
    smtp.set_debuglevel(1)
    smtp.ehlo(host_server)
    smtp.login(sender_qq, pwd)

    msg = MIMEText(mail_content, "html", 'utf-8')
    msg["Subject"] = Header(mail_title, 'utf-8')
    msg["From"] = sender_qq_mail
    msg["To"] = receiver
    smtp.sendmail(sender_qq_mail, receiver, msg.as_string())
    smtp.quit()

    # 没有发过就添加
    # # 正则判断邮箱
    # qq_email = re.search('[0-9]{8,12}@qq.com', receiver)
    # if qq_email:
    #     # 调用发验证码功能
    #     login_email(receiver)
    #     # 响应成功数据
    #     data = {
    #         "msg": "请求成功",
    #         "email": receiver,
    #         "status": 200,
    #         "data": {
    #             "code": code,
    #             "data": "验证码发送成功",
    #         }
    #     }
    #     json_data = json.dumps(data, ensure_ascii=False)
    #     # 获取客户端ip地址
    #     ip = request.META.get('REMOTE_ADDR')
    #     print(f"客户端ip地址为：{ip}")
    #
    #     return HttpResponse(json_data, content_type="application/json")
    # else:
    #     # 获取客户端ip地址
    #     ip = request.META.get('REMOTE_ADDR')
    #     print(f"客户端ip地址为：{ip}")
    #     data = {
    #         "msg": "请求失败",
    #         "status": 400,
    #         "data": {
    #             "data": "QQ邮箱格式错误"
    #         }
    #     }
    #     json_data = json.dumps(data, ensure_ascii=False)
    #
    #     return HttpResponse(json_data, content_type="application/json")
